package com.xindaoapp.framework.activity;

import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.utils.UIHandler;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xindaoapp.framework.R;
import com.xindaoapp.framework.base.BaseActivity;

/**
 * ============================================================
 * 
 * 版权 : 新道APP (c) 2014
 * 
 * 作者 : 贺奕凯
 * 
 * 版本 : 1.0
 * 
 * 创建日期 : 2014-6-24 下午4:19:41
 * 
 * 描述 : 登陆界面
 * 
 * 修订历史 ：
 * 
 * ============================================================
 **/
public class LoginActivity extends BaseActivity implements Callback,
		OnClickListener, PlatformActionListener {
	@ViewInject(R.id.tittle_common_register)
	private TextView tittle_common_register;
	@ViewInject(R.id.tittle_common_cancel)
	private ImageView tittle_common_cancel;
	@ViewInject(R.id.login_sina)
	private RelativeLayout login_sina;
	@ViewInject(R.id.login_qq)
	private RelativeLayout login_qq;
	private static final int MSG_USERID_FOUND = 1;
	private static final int MSG_LOGIN = 2;
	private static final int MSG_AUTH_CANCEL = 3;
	private static final int MSG_AUTH_ERROR = 4;
	private static final int MSG_AUTH_COMPLETE = 5;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		ShareSDK.initSDK(this);
		setContentView(R.layout.activity_login);
		ViewUtils.inject(this); // 加载注释控件

		setListener();
	}

	private void setListener() {
		login_qq.setOnClickListener(this);
		login_sina.setOnClickListener(this);
		tittle_common_cancel.setOnClickListener(this);
		tittle_common_register.setOnClickListener(this);
	}

	@Override
	public void onCancel(Platform platform, int action) {
		if (action == Platform.ACTION_USER_INFOR) {
			UIHandler.sendEmptyMessage(MSG_AUTH_CANCEL, this);
		}
	}

	@Override
	public void onComplete(Platform platform, int action,
			HashMap<String, Object> res) {
		if (action == Platform.ACTION_USER_INFOR) {
			UIHandler.sendEmptyMessage(MSG_AUTH_COMPLETE, this);
			login(platform.getName(), platform.getDb().getUserId(), res);
		}
		System.out.println(res);
	}

	@Override
	public void onError(Platform platform, int action, Throwable t) {
		if (action == Platform.ACTION_USER_INFOR) {
			UIHandler.sendEmptyMessage(MSG_AUTH_ERROR, this);
		}
		t.printStackTrace();
	}

	/**
	 * 点击事件
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_sina: 
			authorize(new SinaWeibo(this));
			break;
		case R.id.login_qq: 
			authorize(new QZone(this));
			break;
		case R.id.tittle_common_cancel: 
			finish();
		case R.id.tittle_common_register: 
			Intent intent = new Intent(getBaseContext(),RegisterActivity.class);
			startActivity(intent);
			break;
		}
	}

	private void authorize(Platform plat) {

		if (plat.isValid()) {
			String userId = plat.getDb().getUserId();
			System.out.println("用户为：" + userId);
			if (!TextUtils.isEmpty(userId)) {
				UIHandler.sendEmptyMessage(MSG_USERID_FOUND, this);
				login(plat.getName(), userId, null);
				return;
			}
		}
		plat.setPlatformActionListener(this);
		plat.SSOSetting(true);
		plat.showUser(null);
	}

	private void login(String plat, String userId,
			HashMap<String, Object> userInfo) {
		Message msg = new Message();
		msg.what = MSG_LOGIN;
		msg.obj = plat;
		System.out.println(plat);
		UIHandler.sendMessage(msg, this);
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case MSG_USERID_FOUND: {
			Toast.makeText(this, R.string.userid_found, Toast.LENGTH_SHORT)
					.show();
		}
			break;
		case MSG_LOGIN: {

			String text = getString(R.string.logining, msg.obj);
			Toast.makeText(this, text, Toast.LENGTH_SHORT).show();

			// Builder builder = new Builder(this);
			// builder.setTitle(R.string.if_register_needed);
			// builder.setMessage(R.string.after_auth);
			// builder.setPositiveButton(R.string.ok, null);
			// builder.create().show();
		}
			break;
		case MSG_AUTH_CANCEL: {
			Toast.makeText(this, R.string.auth_cancel, Toast.LENGTH_SHORT)
					.show();
		}
			break;
		case MSG_AUTH_ERROR: {
			Toast.makeText(this, R.string.auth_error, Toast.LENGTH_SHORT)
					.show();
		}
			break;
		case MSG_AUTH_COMPLETE: {
			Toast.makeText(this, R.string.auth_complete, Toast.LENGTH_SHORT)
					.show();
		}
			break;
		}
		return false;
	}

	@Override
	protected void onDestroy() {
		ShareSDK.stopSDK(this);
		super.onDestroy();
	}

}
